package com.phz.springcloud.study.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.phz.springcloud.study.vo.CreditScoreResponse;

@Service
@Transactional
public class ScoreService {
	
	protected static final Logger logger = LoggerFactory.getLogger(ScoreService.class);
	
	private static final ObjectMapper mapper = new ObjectMapper();
	
	public static final String SCORE_ROUTINGKEY = "score-queue";
	
	private static final List<CreditScoreResponse> listScore = Lists.newArrayList(
			new CreditScoreResponse(1,"882.287"),
			new CreditScoreResponse(1,"425.276"),
			new CreditScoreResponse(1,"128.172"),
			new CreditScoreResponse(1,"405.294"),
			new CreditScoreResponse(1,"177.292"),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,"")
			);
	
	private static final List<CreditScoreResponse> listNotFoundScore = Lists.newArrayList(
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,""),
			new CreditScoreResponse(2,"")
			);
	
	
	private static final List<CreditScoreResponse> listErrorScore = Lists.newArrayList(
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,""),
			new CreditScoreResponse(1302,"")
			);
	

	 @Autowired
	 private AmqpTemplate rabbitTemplate;
	 
	
	
	/**
	 * 普通分数
	 * @throws AmqpException
	 * @throws JsonProcessingException
	 */
	public  void generateNormalScore() throws AmqpException, JsonProcessingException{
		rabbitTemplate.convertAndSend(SCORE_ROUTINGKEY, mapper.writeValueAsString(listScore.get((int) (Math.random() * 10))));
	}
	
	/**
	 * 暂无手机号分数
	 * @throws AmqpException
	 * @throws JsonProcessingException
	 */
	public  void generateNotFoundScore() throws AmqpException, JsonProcessingException{
		rabbitTemplate.convertAndSend(SCORE_ROUTINGKEY, mapper.writeValueAsString(listNotFoundScore.get((int) (Math.random() * 10))));
	}
	
	/**
	 * 服务错误
	 * @throws AmqpException
	 * @throws JsonProcessingException
	 */
	public  void generateErrorScore() throws AmqpException, JsonProcessingException{
		rabbitTemplate.convertAndSend(SCORE_ROUTINGKEY, mapper.writeValueAsString(listErrorScore.get((int) (Math.random() * 10))));
	}

}
